import $ from "jquery";
// 获取键盘事件
// function keyboard (eve) {
// console.log('键盘点击', eve)
// }
// document.addEventListener('keydown', keyboard)
// document.onkeydown = keyboard
// 获取浏览器高度
var browserHeight = 0;
const timer = setInterval(() => {
  if (document) {
    clearInterval(timer);
    // browserHeight = document.body.clientHeight
    browserHeight = window.innerHeight;
    // console.log('浏览器高度', browserHeight)
  }
}, 50);
// 设置过自适应高度的节点集合
let htmlCollectionArr = [];
// 监听浏览器大小改变
window.addEventListener("resize", () => {
  htmlCollectionArr = htmlCollectionArr.filter((obj) => {
    let dom;
    if (obj.dom) {
      dom = $(obj.dom);
    } else if (obj.idOrClass) {
      dom = $(obj.idOrClass);
    }
    return dom.length > 0;
  });
  // 重新获取浏览器高
  browserHeight = window.innerHeight;
  for (let i = 0; i < htmlCollectionArr.length; i++) {
    const element = htmlCollectionArr[i];
    if (element.methodName === "domCurrentPosition") {
      if ($(element.dom).length > 0) {
        domCurrentPosition(element.dom, true);
      }
    } else if (element.methodName === "domCurrentPositionPro") {
      if ($(element.idOrClass).length > 0) {
        domCurrentPositionPro(element.idOrClass, element.isAutoSetting, element.callback, true);
      }
    } else if (element.methodName === "domHeightRelativeParentDom") {
      if ($(element.idOrClass).length > 0) {
        domHeightRelativeParentDom(
          element.idOrClass,
          element.isAutoSetting,
          element.percent,
          element.isExceptSiblings,
          element.callback,
          true
        );
      }
    } else if (element.methodName === "domAdaptHeight") {
      if ($(element.dom).length > 0) {
        domAdaptHeight(element.dom, element.isAutoSetting, element.callback, true);
      }
    } else if (element.methodName === "domAdaptHeightRelativeParent") {
      if ($(element.dom).length > 0) {
        domAdaptHeightRelativeParent(
          element.dom,
          element.isAutoSetting,
          element.percent,
          element.isExceptSiblings,
          element.callback,
          true
        );
      }
    }
  }
});
/**
 * 获取当前元素在浏览器中的位置及自适应高度
 * @param {HTMLCollection} obj Dom节点
 * @param {Boolean} isListener 是否监听调用，默认页面中使用全部为false，只有监听调用时为true
 * @returns 返回计算结果，{top: 74, left: 466, bottom: 10, height: 853}
 */
const domCurrentPosition = (obj, isListener) => {
  const passArr = htmlCollectionArr.filter((item) => {
    let selectorName = "";
    if (item.dom) {
      if (item.dom.id) {
        selectorName = "#" + item.dom.id;
      } else {
        // 完整类名
        for (let i = 0; i < item.dom.classList.length; i++) {
          const element = item.dom.classList[i];
          selectorName += "." + element;
        }
      }
      return $(selectorName).is($(obj));
    }
  });
  if (!isListener && passArr.length === 0) {
    const param = {
      methodName: "domCurrentPosition",
      dom: obj,
    };
    htmlCollectionArr.push(param);
  }
  // 调用获取左上
  const tlResult = getElementTopLeft(obj);
  // 调用获取底部
  const bResult = getElementBottom(obj);
  // 减去距离上部距离和下部距离
  const height = browserHeight - tlResult.top - bResult.bottom;
  let result = {};
  result = Object.assign(result, tlResult);
  result = Object.assign(result, bResult);
  result = Object.assign(result, {
    height: height,
  });
  return result;
};

/**
 * 新的元素自适应高度及获取元素位置的方法
 * @param {String} idOrClass 元素的id或者class都可以，'#div'或'.div'
 * @param {Boolean} isAutoSetting 是否将计算出来的元素高度直接给元素设置，true直接设置，false不设置
 * @param {function} callback 回调方法，返回计算结果，{top: 顶部距离, left: 左侧距离, bottom: 底部距离, height: 元素高度}
 * @param {Boolean} isListener 是否监听调用，默认页面中使用全部为false，只有监听调用时为true
 */
const domCurrentPositionPro = (idOrClass, isAutoSetting, callback, isListener) => {
  const passArr = htmlCollectionArr.filter((item) => {
    return $(item.idOrClass).is($(idOrClass));
  });
  if (!isListener && passArr.length === 0) {
    const param = {
      methodName: "domCurrentPositionPro",
      idOrClass: idOrClass,
      isAutoSetting: isAutoSetting,
      callback: callback,
    };
    htmlCollectionArr.push(param);
  }
  let obj;
  let result = {};
  const timer = setInterval(() => {
    // 判断传进来的是id还是class
    if (idOrClass.indexOf("#") >= 0) {
      // id
      const idObj = document.getElementById(idOrClass.split("#")[1]);
      if (idObj) {
        clearInterval(timer);
        obj = idObj;
      }
    } else if (idOrClass.indexOf(".") >= 0) {
      // class
      // const classObj = document.getElementsByClassName(idOrClass.split('.')[1]);
      const classObj = document.querySelectorAll(idOrClass);
      if (classObj.length > 0) {
        clearInterval(timer);
        obj = classObj[classObj.length - 1];
      }
    }
    if (obj) {
      // 调用获取左上
      const tlResult = getElementTopLeft(obj);
      // 调用获取底部
      const bResult = getElementBottom(obj);

      // 减去距离上部距离和下部距离
      const height = browserHeight - tlResult.top - bResult.bottom;
      if (isAutoSetting) {
        obj.style.height = height + "px";
      }
      result = Object.assign(result, tlResult);
      result = Object.assign(result, bResult);
      result = Object.assign(result, {
        height: height,
      });
      if (typeof callback === "function") {
        callback(result);
      }
    }
  }, 50);
};

/**
 * 元素自适应高度及获取元素位置的方法
 * @param {HTMLCollection} obj Dom节点
 * @param {Boolean} isAutoSetting 是否将计算出来的元素高度直接给元素设置，true直接设置，false不设置
 * @param {function} callback 回调方法，返回计算结果，{top: 顶部距离, left: 左侧距离, bottom: 底部距离, height: 元素高度}
 * @param {Boolean} isListener 是否监听调用，默认页面中使用全部为false，只有监听调用时为true
 */
const domAdaptHeight = (obj, isAutoSetting, callback, isListener) => {
  const passArr = htmlCollectionArr.filter((item) => {
    let selectorName = "";
    if (item.dom) {
      if (item.dom.id) {
        selectorName = "#" + item.dom.id;
      } else {
        // 完整类名
        for (let i = 0; i < item.dom.classList.length; i++) {
          const element = item.dom.classList[i];
          selectorName += "." + element;
        }
      }
      return $(selectorName).is($(obj));
    }
  });
  if (!isListener && passArr.length === 0) {
    const param = {
      methodName: "domAdaptHeight",
      dom: obj,
      isAutoSetting: isAutoSetting,
      callback: callback,
    };
    htmlCollectionArr.push(param);
  }
  let result = {};
  if (obj) {
    // 调用获取左上
    const tlResult = getElementTopLeft(obj);
    // 调用获取底部
    const bResult = getElementBottom(obj);
    // 减去距离上部距离和下部距离
    const height = browserHeight - tlResult.top - bResult.bottom;
    if (isAutoSetting) {
      obj.style.height = height + "px";
    }
    result = Object.assign(result, tlResult);
    result = Object.assign(result, bResult);
    result = Object.assign(result, {
      height: height,
    });
    if (typeof callback === "function") {
      callback(result);
    }
  }
};

/**
 * 设置元素相对于父级元素的自适应高度
 * @param {String} idOrClass 元素的id或者class都可以，'#div'或'.div'
 * @param {Boolean} isAutoSetting 是否将计算出来的元素高度直接给元素设置，true直接设置，false不设置
 * @param {String} percent 设置计算出高度的百分比
 * @param {Boolean} isExceptSiblings 是否去掉兄弟元素高度
 * @param {function} callback 回调方法，返回计算结果，{height: 元素高度}
 * @param {Boolean} isListener 是否监听调用，默认页面中使用全部为false，只有监听调用时为true
 */
const domHeightRelativeParentDom = (idOrClass, isAutoSetting, percent, isExceptSiblings, callback, isListener) => {
  const passArr = htmlCollectionArr.filter((item) => {
    return $(item.idOrClass).is($(idOrClass));
  });
  if (!isListener && passArr.length === 0) {
    const param = {
      methodName: "domHeightRelativeParentDom",
      idOrClass: idOrClass,
      isAutoSetting: isAutoSetting,
      percent: percent,
      isExceptSiblings: isExceptSiblings,
      callback: callback,
    };
    htmlCollectionArr.push(param);
  }
  let obj;
  const result = {};
  // 转换百分比为小数
  const percentTransformation = percent.split("%")[0] / 100;
  const timer = setInterval(() => {
    // 判断传进来的是id还是class
    if (idOrClass.indexOf("#") >= 0) {
      // id
      const idObj = document.getElementById(idOrClass.split("#")[1]);
      if (idObj) {
        clearInterval(timer);
        obj = idObj;
      }
    } else if (idOrClass.indexOf(".") >= 0) {
      // class
      // const classObj = document.getElementsByClassName(idOrClass.split('.')[1]);
      const classObj = document.querySelectorAll(idOrClass);
      if (classObj.length > 0) {
        // if (classObj) {
        clearInterval(timer);
        obj = classObj[classObj.length - 1];
      }
    }
    if (obj) {
      // 获取父级元素
      const parentObj = obj.parentNode;
      // console.log('父级节点', parentObj);
      if (parentObj) {
        // 获取父级元素高度
        const parentObjHeight = parentObj.clientHeight;
        // console.log('父级元素高度', parentObjHeight);
        // 获取所有兄弟节点，用父级的所有子节点，去掉自己，就是所有兄弟节点
        // 计算所有兄弟节点高度
        let siblingsHeight = 0;
        // 计算父节点padding
        const computedStyleParent = document.defaultView.getComputedStyle(parentObj, null);
        // console.log('拿到父节点的样式', computedStyleParent);
        if (computedStyleParent.length > 0) {
          const paddingBottom = parseFloat(
            window.ActiveXObject || "ActiveXObject" in window
              ? computedStyleParent["paddingBottom"].replace("px", "")
              : computedStyleParent["padding-bottom"].replace("px", "")
          );
          const paddingTop = parseFloat(
            window.ActiveXObject || "ActiveXObject" in window
              ? computedStyleParent["paddingTop"].replace("px", "")
              : computedStyleParent["padding-top"].replace("px", "")
          );
          // console.log('计算padding', paddingTop, paddingBottom);
          siblingsHeight += paddingBottom + paddingTop;
        }
        // 是否去掉兄弟节点高度
        if (isExceptSiblings) {
          // console.log('兄弟节点', parentObj.children, $(obj).siblings());
          for (let i = 0; i < parentObj.children.length; i++) {
            const element = parentObj.children[i];
            if (obj !== element) {
              // console.log('兄弟节点高度', element, element.clientHeight);
              siblingsHeight += element.clientHeight;
              // 计算兄弟节点border
              const computedStyle = document.defaultView.getComputedStyle(element, null);
              if (computedStyle.length > 0) {
                const borderBottom = parseFloat(
                  window.ActiveXObject || "ActiveXObject" in window
                    ? computedStyle["borderBottom"]
                      ? computedStyle["borderBottom"]
                      : "0"
                    : computedStyle["border-bottom"]
                );
                const borderTop = parseFloat(
                  window.ActiveXObject || "ActiveXObject" in window
                    ? computedStyle["borderTop"]
                      ? computedStyle["borderTop"]
                      : "0"
                    : computedStyle["border-top"]
                );
                // 计算兄弟节点margin
                const marginBottom = parseFloat(
                  window.ActiveXObject || "ActiveXObject" in window
                    ? computedStyle["marginBottom"].replace("px", "")
                    : computedStyle["margin-bottom"].replace("px", "")
                );
                const marginTop = parseFloat(
                  window.ActiveXObject || "ActiveXObject" in window
                    ? computedStyle["marginTop"].replace("px", "")
                    : computedStyle["margin-top"].replace("px", "")
                );
                siblingsHeight += borderBottom + borderTop + marginBottom + marginTop;
              }
            }
          }
        }
        // 计算元素高度=父级高度-所有兄弟高度
        result.height = (parentObjHeight - siblingsHeight) * percentTransformation;
        if (isAutoSetting) {
          obj.style.height = result.height + "px";
        }
      }
      if (typeof callback === "function") {
        callback(result);
      }
    }
  }, 50);
};

/**
 * 设置元素相对于父级元素的自适应高度
 * @param {HTMLCollection} obj Dom节点
 * @param {Boolean} isAutoSetting 是否将计算出来的元素高度直接给元素设置，true直接设置，false不设置
 * @param {String} percent 设置计算出高度的百分比
 * @param {Boolean} isExceptSiblings 是否去掉兄弟元素高度
 * @param {function} callback 回调方法，返回计算结果，{height: 元素高度}
 * @param {Boolean} isListener 是否监听调用，默认页面中使用全部为false，只有监听调用时为true
 */
const domAdaptHeightRelativeParent = (obj, isAutoSetting, percent, isExceptSiblings, callback, isListener) => {
  const passArr = htmlCollectionArr.filter((item) => {
    let selectorName = "";
    if (item.dom) {
      if (item.dom.id) {
        selectorName = "#" + item.dom.id;
      } else {
        // 完整类名
        for (let i = 0; i < item.dom.classList.length; i++) {
          const element = item.dom.classList[i];
          selectorName += "." + element;
        }
      }
      return $(selectorName).is($(obj));
    }
  });
  if (!isListener && passArr.length === 0) {
    const param = {
      methodName: "domAdaptHeightRelativeParent",
      dom: obj,
      isAutoSetting: isAutoSetting,
      percent: percent,
      isExceptSiblings: isExceptSiblings,
      callback: callback,
    };
    htmlCollectionArr.push(param);
  }
  const result = {};
  // 转换百分比为小数
  const percentTransformation = percent.split("%")[0] / 100;
  if (obj) {
    // 获取父级元素
    const parentObj = obj.parentNode;
    if (parentObj) {
      // 获取父级元素高度
      const parentObjHeight = parentObj.clientHeight;
      // 获取所有兄弟节点，用父级的所有子节点，去掉自己，就是所有兄弟节点
      // 计算所有兄弟节点高度
      let siblingsHeight = 0;
      // 计算父节点padding
      const computedStyleParent = document.defaultView.getComputedStyle(parentObj, null);
      if (computedStyleParent.length > 0) {
        const paddingBottom = parseFloat(computedStyleParent["padding-bottom"].replace("px", ""));
        const paddingTop = parseFloat(computedStyleParent["padding-top"].replace("px", ""));
        siblingsHeight += paddingBottom + paddingTop;
      }
      // 是否去掉兄弟节点高度
      if (isExceptSiblings) {
        for (let i = 0; i < parentObj.children.length; i++) {
          const element = parentObj.children[i];
          if (obj !== element) {
            siblingsHeight += element.clientHeight;
            // 计算兄弟节点border
            const computedStyle = document.defaultView.getComputedStyle(element, null);
            if (computedStyle.length > 0) {
              const borderBottom = parseFloat(computedStyle["border-bottom"]);
              const borderTop = parseFloat(computedStyle["border-top"]);
              siblingsHeight += borderBottom + borderTop;
            }
          }
        }
      }
      // 计算元素高度=父级高度-所有兄弟高度
      result.height = (parentObjHeight - siblingsHeight) * percentTransformation;
      if (isAutoSetting) {
        obj.style.height = result.height + "px";
      }
    }
    if (typeof callback === "function") {
      callback(result);
    }
  }
};

/**
 * 获取元素距离上部及左侧距离
 * @param {HTMLCollection} obj Dom节点
 * @returns {Object} 返回计算结果，{top: 顶部距离, left: 左侧距离}
 */
const getElementTopLeft = (obj) => {
  // let t = obj.offsetTop // 获取该元素对应父容器的上边距
  const t = $(obj).offset().top;
  // let l = obj.offsetLeft // 对应父容器的上边距
  const l = $(obj).offset().left;
  // 判断是否有父容器，如果存在则累加其边距
  // while ((obj = obj.offsetParent)) {
  //   t += obj.offsetTop
  //   l += obj.offsetLeft
  // }
  const result = {
    top: t,
    left: l,
  };
  return result;
};

/**
 * 获取元素距离底部距离(margin和padding)
 * @param {HTMLCollection} obj Dom节点
 * @returns {Object} 返回计算结果，{bottom: 底部距离}
 */
const getElementBottom = (obj) => {
  let b = 0;
  while ((obj = obj.parentNode)) {
    if (obj.localName) {
      if (obj.currentStyle) {
        // console.log('currentStyle', obj.currentStyle)
      }
      const computedStyle = document.defaultView.getComputedStyle(obj, null);
      // console.log('computedStyle', computedStyle)
      if (computedStyle.length > 0) {
        const margin = parseFloat(
          window.ActiveXObject || "ActiveXObject" in window
            ? computedStyle["marginBottom"].replace("px", "")
            : computedStyle["margin-bottom"].replace("px", "")
        );
        const padding = parseFloat(
          window.ActiveXObject || "ActiveXObject" in window
            ? computedStyle["paddingBottom"].replace("px", "")
            : computedStyle["padding-bottom"].replace("px", "")
        );
        const borderBottom = parseFloat(
          window.ActiveXObject || "ActiveXObject" in window
            ? computedStyle["borderBottom"]
              ? computedStyle["borderBottom"]
              : "0"
            : computedStyle["border-bottom"]
        );
        // console.log('borderBottom', borderBottom)
        b += margin + padding + borderBottom;
      }
    }
  }
  // console.log('底部结果', b)
  const result = {
    bottom: b,
  };
  return result;
};

/**
 * 将数组转成树结构
 * @param {Array} data 数组
 * @param {String} key 依据哪个字段分层级
 * @param {String} parentPropName 层级字段名称
 * @param {String} childrenName 子集集合名称
 * @returns {Array} 返回结果，树形结构
 */
const listToTree = (data, key, parentPropName, childrenName) => {
  const result = [];
  data.forEach((item) => {
    // delete item[childrenName]
    item[childrenName] = [];
  });
  const map = {};
  data.forEach((item) => {
    map[item[key]] = item;
  });
  data.forEach((item) => {
    const parent = map[item[parentPropName]];
    if (parent) {
      (parent[childrenName] || (parent[childrenName] = [])).push(item);
    } else {
      result.push(item);
    }
  });
  return result;
};

/**
 * 树转数组
 * @param {Array} treeData 树结构数据
 * @param {String} childrenName 子集集合名称
 * @returns {Array} 返回结果，数组
 */
const treeToList = (treeData, childrenName) => {
  const stack = treeData;
  const data = [];
  while (stack.length !== 0) {
    const pop = stack.pop();
    data.unshift(pop);
    const children = pop[childrenName];
    if (children) {
      for (let i = children.length - 1; i >= 0; i--) {
        stack.unshift(children[i]);
      }
    }
  }
  return data;
};

/**
 * @description: 自定义菜单
 * @param {{HTMLCollection}} dom dom节点
 * @param {String} backDivId 选项的父级节点 必须是id
 * @param {Object} style 样式对象
 * @param {String} style.selectBorderColor 选中边框色 '#338fe5'
 * @param {String} style.selectBorderThick 选中边框粗细 '3px'
 * @param {String} style.selectBackRgb 选中背景色 '#338FE5' 必须16进制
 * @param {String} style.selectFontColor 选中字体颜色 '#338fe5'
 * @param {String} style.hoverColor 选项划过背景色 '#f8f8f8'
 * @param {String} style.optionHeight 选项高度 '30px'
 * @param {String} style.fontSize 选项字体大小 '14px'
 * @return {*}
 */
const customMenu = (dom, backDivId, style) => {
  clearInterval(timer);
  // 生成背景div的id
  // const backDivId = getUUID()
  // console.log('背景唯一id', backDivId)
  // 首先处理参数
  // 选中边框色
  const selectBorderColor = style && style.selectBorderColor ? style.selectBorderColor : "#338fe5";
  // 选中边框粗细
  const selectBorderThick = style && style.selectBorderThick ? style.selectBorderThick : "3px";
  // 选中背景色
  const selectBackRgb = style && style.selectBackRgb ? set16ToRgb(style.selectBackRgb) : "51, 143, 229";
  // 选中字体颜色
  const selectFontColor = style && style.selectFontColor ? style.selectFontColor : "#338fe5";
  // 选项划过背景色
  const hoverColor = style && style.hoverColor ? style.hoverColor : "#f8f8f8";
  // 选项高度
  const optionHeight = style && style.optionHeight ? style.optionHeight : "30px";
  // 选项字体大小
  const fontSize = style && style.fontSize ? style.fontSize : "14px";
  // 获取到所有的选项
  const options = $(dom).children();
  // 设置每一个选项初始样式
  options.css({
    padding: "0px 20px",
    height: optionHeight,
    lineHeight: optionHeight,
    fontWeight: "500",
    cursor: "pointer",
    fontSize: fontSize,
  });
  // 设置每一个选项鼠标划过样式
  options.hover(
    function () {
      $(this).css("background", hoverColor);
    },
    function () {
      $(this).css("background", "transparent");
    }
  );
  // 向页面中添加一个背景的div
  if ($("#" + backDivId + "-back").length === 0) {
    $("#" + backDivId).append('<div id="' + backDivId + '-back" style="display:none"></div>');
  }
  // 获取背景div
  const backDiv = $("#" + backDivId + "-back");
  // 设置背景div的样式
  backDiv.css({
    borderLeft: "solid " + selectBorderColor + " " + selectBorderThick,
    background: "rgba(" + selectBackRgb + ", 0.1)",
    position: "absolute",
    minHeight: optionHeight,
    zIndex: "9",
    cursor: "pointer",
  });
  // 设置背景div的划过
  backDiv.hover(
    function () {
      $(this).css("background", "rgba(" + selectBackRgb + ", 0.1)");
    },
    function () {
      $(this).css("background", "rgba(" + selectBackRgb + ", 0.1)");
    }
  );
  // 获取第一个选项
  const firstOption = options.first();
  // 获取第一个选项的位置
  const top = firstOption.offset().top;
  const left = firstOption.offset().left;
  const width = firstOption.outerWidth();
  const height = firstOption.outerHeight();
  // 设置字体颜色
  firstOption.css("color", selectFontColor);
  // 定位背景div
  backDiv.css("top", top + "px");
  backDiv.css("left", left + "px");
  backDiv.css("width", width + "px");
  backDiv.css("height", height + "px");
  backDiv.show();
  // }, 10)
  // 设置每一个选项的点击事件
  options.click(function () {
    // 所有节点字体颜色设置为初始
    options.css("color", "#303030");
    // 获取点击节点
    const clickMenu = $(this);
    // 获取点击元素的位置
    const top = clickMenu.offset().top;
    const left = clickMenu.offset().left;
    const width = clickMenu.outerWidth();
    const height = clickMenu.outerHeight();
    backDiv.css("width", width + "px");
    backDiv.css("height", height + "px");
    // 移动到点击节点的位置
    backDiv.animate(
      {
        top: top + "px",
        left: left + "px",
      },
      200,
      function () {
        // 设置字体颜色
        clickMenu.css("color", selectFontColor);
      }
    );
  });
};
/**
 * @description: 16进制转rgb
 * @param str 16进制颜色字符串
 * @return {*}
 */
const set16ToRgb = (str) => {
  var reg = /^#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$/;
  if (!reg.test(str)) {
    return;
  }
  // eslint-disable-next-line no-useless-escape
  let newStr = str.toLowerCase().replace(/\#/g, "");
  const len = newStr.length;
  if (len === 3) {
    let t = "";
    for (var i = 0; i < len; i++) {
      t += newStr.slice(i, i + 1).concat(newStr.slice(i, i + 1));
    }
    newStr = t;
  }
  const arr = []; // 将字符串分隔，两个两个的分隔
  for (let i = 0; i < 6; i = i + 2) {
    const s = newStr.slice(i, i + 2);
    arr.push(parseFloat("0x" + s));
  }
  return arr.join(",");
};

// 生成uuid
const getUUID = () => {
  var s = [];
  var hexDigits = "0123456789abcdef";
  for (var i = 0; i < 36; i++) {
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  }
  s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
  s[8] = s[13] = s[18] = s[23] = "-";
  var uuid = s.join("");
  return uuid;
};

// // 绑定滚动事件
// const addScrollListener = (el) => {
//   el.addEventListener('scroll', () => {
//     let clientHeight = el.clientHeight;
//     let scrollTop = el.scrollTop;
//     let scrollHeight = el.scrollHeight;
//     console.log('滚动触发', clientHeight, scrollTop, scrollHeight);
//     if (clientHeight + scrollTop >= scrollHeight) {
//       console.log('到底了');
//     }
//     if (scrollTop === 0) {
//       console.log('到顶了');
//     }
//   });
// };

// // 解绑滚动事件
// const removeScrollListener = (el) => {
//   el.removeEventListener('scroll', () => {
//     let clientHeight = el.clientHeight;
//     let scrollTop = el.scrollTop;
//     let scrollHeight = el.scrollHeight;
//     console.log('滚动触发', clientHeight, scrollTop, scrollHeight);
//     if (clientHeight + scrollTop >= scrollHeight) {
//       console.log('到底了');
//     }
//     if (scrollTop === 0) {
//       console.log('到顶了');
//     }
//   });
// };

export default function (app) {
  app.config.globalProperties.$someUtil = {
    domCurrentPosition,
    domCurrentPositionPro, // 根据浏览器可视高度自适应高度
    domHeightRelativeParentDom, // 根据父级高度自适应高度
    domAdaptHeight,
    domAdaptHeightRelativeParent,
    listToTree, // 集合转树
    treeToList, // 树转集合
    customMenu, // 自定义菜单，横向纵向
    set16ToRgb, // 颜色16进制转rgb
    getUUID, // 唯一标识
    browserHeight,
  };
}
